package com.example.mylibrary;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.mylibrary.utils.DisplayUtil;


public class MyBall extends androidx.appcompat.widget.AppCompatImageView {
    private int left,top,width,height;
    private float fingerX,fingerY;
    public MyBall(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }






    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                left=this.getLeft();
                top=this.getTop();
                width=this.getWidth();
                height=this.getHeight();
                fingerX=event.getRawX();
                fingerY=event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                float rawX = event.getRawX();
                float rawY = event.getRawY();
                int l=(int)(rawX+left-fingerX);
                int t=(int)(rawY+top-fingerY);
                int r=(int) (rawX+left+width-fingerX);
                int b=(int) (rawY+top+height-fingerY);
                int winWidth = getResources().getDisplayMetrics().widthPixels;
                int winHeight = getResources().getDisplayMetrics().heightPixels;
                if (l>0&&t>0&&r<winWidth&&b<winHeight){
                    layout(l,t,r,b);
                }
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
        }
        return true;
    }
}
